package xhwl.insurance.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.utils.Log;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;
import xhwl.android.common.util.ToastUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.adapter.SearchAdapter;
import xhwl.insurance.adapter.SearchForumAdapter;
import xhwl.insurance.customView.PullToRefreshListView;
import xhwl.insurance.event.FreshForumListEvent;
import xhwl.insurance.http.api.ForumSearchApi;
import xhwl.insurance.http.bean.resp.ForumSearchResp;
import xhwl.insurance.http.bean.resp2.HttpResp;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * Created by Administrator on 2017/10/12.
 * 论坛搜索
 */

public class ForumSearchActivity extends BaseActivity implements View.OnClickListener {
    private PullToRefreshListView mListView;
    private EditText mSearch;
    private TextView mTvCancel;
    private ImageView mCancel, mBack;
    private ArrayList<ForumSearchResp> mList = new ArrayList<>();
    private SearchForumAdapter adapter;
    private String editText;
    private TextView mTvSearch;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
    }

    @Override
    public void initViews() {
        mListView = (PullToRefreshListView) findViewById(R.id.lv_pullToRefreshListView);
        mSearch = (EditText) findViewById(R.id.edit_search);
        mTvCancel = (TextView) findViewById(R.id.tv_cancel);
        mTvSearch = (TextView) findViewById(R.id.tv_search);
        mCancel = (ImageView) findViewById(R.id.iv_search_cancel);
        mBack = (ImageView) findViewById(R.id.iv_search_back);

        mListView.setEnablePullTorefresh(false);
        mListView.setEnablePullLoadMoreDataStatus(false);

        adapter = new SearchForumAdapter(ForumSearchActivity.this, mList);
        mListView.setAdapter(adapter);

        if (mSearch.getText().toString().length() > 0) {
            mCancel.setVisibility(View.VISIBLE);
        }
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent data = new Intent();
                data.putExtra("SearchText",mList.get(position).getPOST_TITLE());
                setResult(2, data);
                finish();
            }
        });


    }

    @Override
    public void initListener() {
        mSearch.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
        mTvSearch.setOnClickListener(this);
        mCancel.setOnClickListener(this);
        mBack.setOnClickListener(this);

        mSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                    getForumSearchData();
                if (s.length() == 0) {
                    mList.clear();
                    adapter.notifyDataSetChanged();
                } else {

                }
                if (mSearch.getText().toString().length() > 0) {
                    mCancel.setVisibility(View.VISIBLE);
//                    mTvCancel.setText("搜索");
                    mTvSearch.setVisibility(View.VISIBLE);
                    mTvCancel.setVisibility(View.INVISIBLE);
                } else {
                    mCancel.setVisibility(View.GONE);
//                    mTvCancel.setText("取消");
                    mTvSearch.setVisibility(View.GONE);
                    mTvCancel.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    /**
     * 搜索
     */
    private void getForumSearchData() {
        String mSearchContent = mSearch.getText().toString();
        ForumSearchApi api = new ForumSearchApi(mSearchContent);
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<HttpResp<ArrayList<ForumSearchResp>>>() {

            @Override
            public void onNext(HttpResp<ArrayList<ForumSearchResp>> arrayListHttpResp) {
                Log.i("liujin", "arrayListHttpResp" + arrayListHttpResp);
                    if (arrayListHttpResp.isOk()) {
                        mList.clear();
                        if (arrayListHttpResp.data != null && arrayListHttpResp.data.size() > 0) {
                            mList.addAll(arrayListHttpResp.data);
                            adapter.notifyDataSetChanged();
                        } else {

                        }
                    } else {
                        ToastUtil.showToast(ForumSearchActivity.this, arrayListHttpResp.message);

                }
            }


            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
            }
        });

    }

    @Override
    public void loadData() {
//        getSearchData();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_search_back:
                finish();
                break;
            case R.id.edit_search:
                break;
            case R.id.tv_cancel:
                EventBus.getDefault().post(new FreshForumListEvent());
                finish();
                break;
            case R.id.iv_search_cancel:
                mSearch.setText("");
                mList.clear();
                adapter.notifyDataSetChanged();
                break;
            case R.id.tv_search:
                Intent  data = new Intent();
                data.putExtra("SearchText",mSearch.getText().toString().trim());
                setResult(3, data);
                finish();
                break;
        }
    }
}
